﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Diagnostics;

namespace Lilium.Web.Mvc
{
	/// <summary>
	/// 
	/// </summary>
	/// <remarks>
	/// Thread Safety
	/// This type is thread safe
	/// </remarks>
	public abstract class ScriptResource
	{
		public void ProcessRequest(ControllerContext context)
		{
			if (context == null)
				throw new ArgumentNullException("context");

			context.HttpContext.Response.ContentType = ContentType;
			CachePolicy.Apply(context);
			OnProcessRequest(context);
		}

		#region public string Name

		private readonly string m_Name;

		public string Name { [DebuggerNonUserCodeAttribute] get { return m_Name; } }

		#endregion

		protected ScriptResource(string name, string contentType, OutputCachePolicy cachePolicy)
		{
			if (string.IsNullOrEmpty(name))
				throw new ArgumentNullOrEmptyException("name");
			if (cachePolicy == null)
				throw new ArgumentNullException("cachePolicy");

			m_Name = name;
			m_CachePolicy = cachePolicy;
			m_ContentType = contentType;
		}

		protected abstract void OnProcessRequest(ControllerContext context);

		#region private OutputCachePolicy CachePolicy

		private readonly OutputCachePolicy m_CachePolicy;

		private OutputCachePolicy CachePolicy { [DebuggerNonUserCodeAttribute] get { return m_CachePolicy; } }

		#endregion

		#region private string ContentType

		private readonly string m_ContentType;

		private string ContentType { [DebuggerNonUserCodeAttribute] get { return m_ContentType; } }

		#endregion
	}
}